package com.nit.bike.mapper.basic;

import com.nit.bike.dto.basic.BikeDTO;
import com.nit.bike.entity.basic.BikeEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface BikeMapper {
    int deleteByPrimaryKey(Long bikeId);

    int insert(BikeEntity record);

    int insertSelective(BikeEntity record);

    BikeEntity selectByPrimaryKey(Long bikeId);

    int updateByPrimaryKeySelective(BikeEntity record);

    int updateByPrimaryKey(BikeEntity record);

    /**
     * 获取表格数据
     */
    @Select({"<script>SELECT *, bike_id AS `key`, s.station_name AS `stationName` " +
            "FROM basic_bike b " +
            "LEFT JOIN basic_station s USING(station_id) " +
            "WHERE TRUE " +
            "<if test='bikeNumber != null'>AND bike_number LIKE CONCAT('%', #{bikeNumber}, '%') </if>" +
            "<if test='bikeState != null'>AND bike_state = #{bikeState} </if>" +
            "<if test='stationId != null'>AND station_id = #{stationId} </if>" +
            "ORDER BY " +
            "<if test='sortField != null'>${sortField} ${sortOrder}, </if>" +
            "bike_id DESC" +
            "</script>"})
    List<BikeDTO> list4Table(
            @Param("bikeNumber") String bikeNumber,
            @Param("bikeState") String bikeState,
            @Param("stationId") String stationId,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder
    ) throws Exception;

    /**
     * 获取表格数据
     */
    @Select({"<script>SELECT *, bike_id AS `key`, s.station_name AS `stationName` " +
            "FROM basic_bike b " +
            "LEFT JOIN basic_station s USING(station_id) " +
            "WHERE station_id = #{stationId}" +
            "<if test='bikeNumber != null'>AND bike_number LIKE CONCAT('%', #{bikeNumber}, '%') </if>" +
            "<if test='bikeState != null'>AND bike_state = #{bikeState} </if>" +
            "ORDER BY " +
            "<if test='sortField != null'>${sortField} ${sortOrder}, </if>" +
            "bike_id ASC" +
            "</script>"})
    List<BikeDTO> listByStation(
            @Param("stationId") String stationId,
            @Param("bikeNumber") String bikeNumber,
            @Param("bikeState") String bikeState,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder
    ) throws Exception;

    /**
     * 根据id获取车辆
     */
    @Select("SELECT * " +
            "FROM basic_bike b " +
            "WHERE bike_id = #{bikeId} " +
            "LIMIT 1")
    BikeDTO queryById(@Param("bikeId") Long bikeId) throws Exception;

    /**
     * 更新车辆状态
     */
    @Update("Update basic_bike set bike_state = #{bikeState} where bike_id = #{bikeId}")
    Integer updateState(@Param("bikeState") Integer bikeState,
                        @Param("bikeId") Long bikeId) throws Exception;

    /**
     * 修改车辆当前骑行记录
     */
    @Update("UPDATE basic_bike " +
            "SET ride_id = #{rideId}, station_id = #{stationId}, bike_state = #{bikeState} " +
            "WHERE bike_id = #{bikeId}")
    void updateRideInfo(@Param("bikeId") Long bikeId,
                        @Param("rideId") Long rideId,
                        @Param("stationId") Long stationId,
                        @Param("bikeState") Integer bikeState) throws Exception;
}
